<template>
  <div class="home-container">
    <!-- 导航搜索栏 -->
<van-nav-bar class="page-nav-bar" fixed>
  <template #title>
<van-button type="info" size="small" round icon="search" class="search-btn" to="/search">搜索</van-button>
  </template>
</van-nav-bar>
<!-- 频道列表 -->
<van-tabs v-model:active="active" animated swipeable class="channel-tabs">
  <van-tab :title="item.name" v-for="item in channels.channels" :key="item.id">
  <!-- 文章列表 -->
   <ArticleList :channel='item'></ArticleList>
  </van-tab>
  <!-- 汉堡 -->
  <template #nav-right>
    <div class="hamburger-btn" @click="show=true">
      <i class="toutiao toutiao-gengduo"></i>
    </div>
    <div class="placeholder">
    </div>
  </template>
</van-tabs>
<!-- 频道编辑弹出层 -->
<van-popup
  v-model:show="show"
  closeable
  position="bottom"
  close-icon-position="top-left"
  :style="{ height: '100%' }"
><ChannelEdit
  :myChannels='channels.channels'
  :active='active'
  @update-active='onUpdateActive'
  @on-addChannel='addMyChannels'
  @splice-myChannels='spliceMyChannels'
 ></ChannelEdit></van-popup>

  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { getUserChannels } from '@/api/user'
import { Toast } from 'vant'
import { useStore } from 'vuex'
import { setItem, getItem } from '@/utils/storage'
import { addUserChannel, deleteUserChannel } from '@/api/channel'
import ChannelEdit from '@/views/home/components/channel-edit.vue'
import ArticleList from '@/views/home/components/article-list.vue'
export default {
  name: 'HomeIndex',
  created () {
    this.loadChannels()
  },
  components: { ArticleList, ChannelEdit },
  setup () {
    const store = useStore()
    const active = ref(0)
    const channels = reactive({ channels: [] })// channels.channels我的频道列表
    const show = ref(false)// 控制弹出层是否显示
    // 获取我的频道
    async function loadChannels () {
      const localChannels = getItem('TOUTIAO_CHANNELS')
      if (!store.state.user && localChannels) { // 没登录并且有本地数据，频道用本地数据
        channels.channels = localChannels
      } else { // 不然就用线上数据
        try {
          const { data } = await getUserChannels()
          channels.channels = data.data.channels
        } catch (err) {
          Toast('获取失败')
        }
      }
    }
    // 切换
    function onUpdateActive (index, myShow = false) {
      active.value = index
      show.value = myShow
    }
    // 添加
    async function addMyChannels (channel) {
      channels.channels.push(channel)
      // 持久化
      if (store.state.user) {
        try {
          await addUserChannel({
            id: channel.id,
            seq: channels.channels.length
          })
        } catch (err) {
          Toast('保存失败')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', channels.channels)
      }
    }
    // 删除
    async function spliceMyChannels (channel, index) {
      channels.channels.splice(index, 1)
      // 持久化
      if (store.state.user) {
        try {
          await deleteUserChannel(channel.id)
        } catch (err) {
          Toast('保存失败')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', channels.channels)
      }
    }
    return {
      active,
      channels,
      show,
      onUpdateActive,
      addMyChannels,
      spliceMyChannels,
      loadChannels
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  padding-top: 174px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: 0;
    font-size: 28px;
    .van-icon{
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs{
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color:#777777
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      background-color: #3296fa;
      width: 31px;
      height: 6px;
    }
    .placeholder{
      width: 49px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-btn{
      position: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      right: 0;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 82px;
        background: url(@/assets/gradient-gray-line.png);
        background-size: contain;
      }
      .toutiao{
        font-size: 33px;
      }
    }
  }
}
</style>
